/**   
 * @Title: TrackingRecrodAdapter.java 
 * @Package: com.esun.rabbit2.viewer.mainact.mylotteryact.trackingrecord 
 * @Description: TODO(追号记录adapter类) 
 * @Author: Billy qigs@500wan.com   
 * @Date: 2011-7-21 下午07:20:19  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.mylotteryact.trackingrecord;

import java.util.ArrayList;
import java.util.Hashtable;

import com.esun.config.Config;
import com.esun.config.Key;
import com.esun.ui.R;
import com.esun.util.log.EDebug;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @ClassName: TrackingRecrodAdapter
 * @Description: TODO(追号记录adapter类)
 * @Author: Billy qigs@500wan.com
 * @Date: 2011-7-21 下午07:20:19
 */
public class TrackingRecrodAdapter extends BaseAdapter {

	private LayoutInflater listInflater;
	private ArrayList<Hashtable<String, String>> list = new ArrayList<Hashtable<String, String>>();

	private LinearLayout linearDate;
	// private LinearLayout linearInfo;

	private TextView tvDate;
	private TextView tvTime;
	private TextView tvLotName;
	private TextView tvTotalNum;
	private TextView tvFinishNum;
	private TextView tvTotalMoney;
	private TextView tvState;
	private ImageView imgLotType;

	private int groupCount = 0;
	private int childCount = 0;

	public TrackingRecrodAdapter(Context context, ArrayList<Hashtable<String, String>> tempList) {
		this.listInflater = LayoutInflater.from(context);
		this.list = tempList;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		convertView = (LinearLayout) listInflater.inflate(R.layout.trackingrecord_listtable, null);

		int[] finishNumBgResources = { R.drawable.icon_default_follownumberlist_golden00_201x63, R.drawable.icon_default_follownumberlist_golden10_201x63,
				R.drawable.icon_default_follownumberlist_golden20_201x63, R.drawable.icon_default_follownumberlist_golden30_201x63, R.drawable.icon_default_follownumberlist_golden40_201x63,
				R.drawable.icon_default_follownumberlist_golden50_201x63, R.drawable.icon_default_follownumberlist_golden60_201x63, R.drawable.icon_default_follownumberlist_golden70_201x63,
				R.drawable.icon_default_follownumberlist_golden80_201x63, R.drawable.icon_default_follownumberlist_golden90_201x63, R.drawable.icon_default_follownumberlist_golden100_201x63 };
		int[] finishNumBgResourcesChedan = { R.drawable.icon_default_follownumberlist_golden00_201x63, R.drawable.icon_default_follownumberlist_s10_201x63,
				R.drawable.icon_default_follownumberlist_s20_201x63, R.drawable.icon_default_follownumberlist_s30_201x63, R.drawable.icon_default_follownumberlist_s40_201x63,
				R.drawable.icon_default_follownumberlist_s50_201x63, R.drawable.icon_default_follownumberlist_s60_201x63, R.drawable.icon_default_follownumberlist_s70_201x63,
				R.drawable.icon_default_follownumberlist_s80_201x63, R.drawable.icon_default_follownumberlist_s90_201x63, R.drawable.icon_default_follownumberlist_s100_201x63

		};
		int resourcesCode = Integer.parseInt(list.get(position).get("finishnum")) * 10 / Integer.parseInt(list.get(position).get("totalnum"));
		EDebug.o("背景图片选择", "resourcesCode = " + resourcesCode);

		linearDate = (LinearLayout) convertView.findViewById(R.id.trackingrecord_listtable_linear_date);
		// linearInfo = (LinearLayout)
		// convertView.findViewById(R.id.trackingrecord_listtable_linear_info);

		tvDate = (TextView) convertView.findViewById(R.id.trackingrecord_listtable_tv_date);
		tvDate.setText(list.get(position).get("date"));

		tvTime = (TextView) convertView.findViewById(R.id.trackingrecord_listtable_tv_time);
		tvTime.setText(list.get(position).get("time"));

		tvLotName = (TextView) convertView.findViewById(R.id.trackingrecord_listtable_tv_lotname);
		tvLotName.setText(list.get(position).get("lotname"));

		EDebug.o(TrackingRecrodAdapter.this, "lot name is " + list.get(position).get("lotname") + "^^^^^^^^^^^^^^^^^^^^^^^");
		imgLotType = (ImageView) convertView.findViewById(R.id.trackingrecord_listtable_img_lottype);
		// 2012-03-01 modified kuroky
		if (list.get(position).get(Key.KEY_LOTID).equals(Config.SHUANGSEQIU)) {
			imgLotType.setBackgroundResource(R.drawable.img_default_boughtcenter_doubleball_53x53);
		} else if (list.get(position).get(Key.KEY_LOTID).equals(Config.DALETOU)) {
			imgLotType.setBackgroundResource(R.drawable.img_default_boughtcenter_lotto_53x53);
		} else if (list.get(position).get(Key.KEY_LOTID).equals(Config.FUCAI3D)) {
			imgLotType.setBackgroundResource(R.drawable.img_default_boughtcenter_3d_53x53);
		} else if (list.get(position).get(Key.KEY_LOTID).equals(Config.QIXINGCAI)) {
			imgLotType.setBackgroundResource(R.drawable.img_default_boughtcenter_7star_53x53);
		} else if (list.get(position).get(Key.KEY_LOTID).equals(Config.ERSHIERXUANWU)) {
			imgLotType.setBackgroundResource(R.drawable.img_default_boughtcenter_22pick5_53x53);
		} else if (list.get(position).get(Key.KEY_LOTID).equals(Config.PAILIESAN)) {
			imgLotType.setBackgroundResource(R.drawable.img_default_boughtcenter_pl3_53x53);
		} else if (list.get(position).get(Key.KEY_LOTID).equals(Config.PAILIEWU)) {
			imgLotType.setBackgroundResource(R.drawable.img_default_boughtcenter_pl5_53x53);
		} else if (list.get(position).get(Key.KEY_LOTID).equals(Config.QILECAI)) {
			imgLotType.setBackgroundResource(R.drawable.img_default_boughtcenter_7le_53x53);
		}
		// 2012-03-01 modified kuroky
		// tvLotName.setVisibility(View.GONE);

		tvTotalNum = (TextView) convertView.findViewById(R.id.trackingrecord_listtable_tv_totalNum);
		tvTotalNum.setText("共" + list.get(position).get("totalnum") + "期");

		tvFinishNum = (TextView) convertView.findViewById(R.id.trackingrecord_listtable_tv_finishNum);
		tvFinishNum.setText(list.get(position).get("finishnum") + "期");

		tvTotalMoney = (TextView) convertView.findViewById(R.id.trackingrecord_listtable_tv_totalMoney);
		tvTotalMoney.setText("￥" + list.get(position).get("totalmoney"));

		tvState = (TextView) convertView.findViewById(R.id.trackingrecord_listtable_tv_state);
		if (list.get(position).get("state").equals("0")) {
			tvState.setText("进行中");
			// tvFinishNum.setBackgroundResource(finishNumBgResources[resourcesCode]);
			((LinearLayout) convertView.findViewById(R.id.trackingrecord_listtable_linear_finishLine)).setBackgroundResource(finishNumBgResources[resourcesCode]);
			tvState.setTextColor(0xffff5a00);
		} else if (list.get(position).get("state").equals("1")) {
			tvState.setText("已完成");
			// tvFinishNum.setBackgroundResource(R.drawable.icon_default_follownumberlist_golden100_201x63);
			((LinearLayout) convertView.findViewById(R.id.trackingrecord_listtable_linear_finishLine)).setBackgroundResource(R.drawable.icon_default_follownumberlist_golden100_201x63);
			tvState.setTextColor(0xff000000);
		} else if (list.get(position).get("state").equals("2")) {
			tvState.setText("已撤单");
			// tvFinishNum.setBackgroundResource(finishNumBgResourcesChedan[resourcesCode]);
			((LinearLayout) convertView.findViewById(R.id.trackingrecord_listtable_linear_finishLine)).setBackgroundResource(finishNumBgResourcesChedan[resourcesCode]);
			tvState.setTextColor(0xff6a6a6a);
		}

		/*
		 * 判断是不是同一天 加载不同的背景 还需要考虑下
		 */
		if (position == 0) {
			linearDate.setVisibility(View.VISIBLE);
			EDebug.o(this, "position=" + position);
			EDebug.o(this, "这是第一条");
		} else if (!list.get(position).get("date").equals(list.get(position - 1).get("date"))) {
			linearDate.setVisibility(View.VISIBLE);
			EDebug.o(this, "position=" + position);
			EDebug.o(this, "与前一条不是同一天");
			EDebug.o(this, "position=" + position + "date=" + list.get(position).get("date"));
			EDebug.o(this, "position-1=" + (position - 1) + "date=" + list.get(position - 1).get("date"));
		} else if (list.get(position).get("date").equals(list.get(position - 1).get("date"))) {
			// linearInfo.setBackgroundResource(R.drawable.anull_icon);
			// LinearLayout preItem = (LinearLayout)
			// parent.findViewById(position - 1);
			// LinearLayout preItemInfo = (LinearLayout)
			// preItem.findViewById(R.id.trackingrecord_listtable_linear_info);
			// preItemInfo.setBackgroundDrawable(null);
			EDebug.o(this, "position=" + position);
			EDebug.o(this, "与上一条记录是同一天");
			EDebug.o(this, "position=" + position + "date=" + list.get(position).get("date"));
			EDebug.o(this, "position-1=" + (position - 1) + "date=" + list.get(position - 1).get("date"));
		} else {
			EDebug.o(this, "position=" + position);
			EDebug.o(this, "其他情况");
			EDebug.o(this, "position=" + position + "date=" + list.get(position).get("date"));
			EDebug.o(this, "position-1=" + (position - 1) + "date=" + list.get(position - 1).get("date"));
		}

		return convertView;
	}

	public void countCalculate() {
		groupCount = 0;
		childCount = 0;
		for (int i = 0; i < list.size(); i++) {
			if (i == 0) {
				groupCount++;
			} else if (!list.get(i).get("date").equals(list.get(i - 1).get("date"))) {
				groupCount++;
			} else {
				childCount++;
			}
		}
	}

	public int getGroupCount() {
		return groupCount;
	}

	public int getChildCount() {
		return childCount;
	}

}
